/**
 * Created by zhaojm on 15/4/23.
 */
game.NailGenerator = cc.Class.extend({
    _layer:null,
    _world:null,
    _timeCount:null,
    _MAX_TIME : 1.5,
    _MIN_TIME : 0.25,
    _timeRandom : null,
    _MAX_COUNT:1,
    _MIN_COUNT:1,
    _countRandom : null,
    _posY:null,
    _timeAllCount:null,
    ctor:function(layer, world, platform){
        this._layer = layer;
        this._world = world;
        this._posY = platform.getPositionY() + platform.getContentSize().height / 2;

        this._timeCount = 0;

        this.doRandom();
    },

    update:function(dt){
        this._timeCount += dt;
        this._timeAllCount += dt;
        var winSize = cc.winSize;
        //cc.log('timeCount=', this._timeCount);
        if(this._timeCount >= this._timeRandom){
            this.generate(game._Camera.x + winSize.width, this._countRandom);
            this.doRandom();
            this._timeCount = 0;
        }

    },

    doRandom : function(){

        var timeAllCount = this._timeAllCount;
        if(timeAllCount <= 10){

        }else if(timeAllCount > 10 && timeAllCount <= 20){
            this._MAX_TIME = 1.2;
            this._MIN_TIME = 0.2;
            this._MAX_COUNT = 2;
        }else if(timeAllCount > 20 && timeAllCount <= 30){
            this._MAX_TIME = 1.0;
            this._MIN_TIME = 0.2;
            this._MAX_COUNT = 2;
        }else if(timeAllCount > 20 && timeAllCount <= 30){
            this._MAX_TIME = 1.0;
            this._MIN_TIME = 0.2;
            this._MAX_COUNT = 2;
        }else{
            this._MAX_TIME = 0.9;
            this._MIN_TIME = 0.1;
            this._MAX_COUNT = 3;
        }


        this._timeRandom = Math.random() * this._MAX_TIME + this._MIN_TIME;
        this._countRandom = Math.floor(Math.random() * this._MAX_COUNT + this._MIN_COUNT);
    },

    generate:function(posX, count){
        var lastNail = null;
        var lastPosX = posX;
        for(var i = count; i >0; i--){
            //cc.log('lastPosX=', lastPosX);

            //lastNail = new game.Nail(cc.p(lastPosX, this._posY));
            lastNail = new game.DynamicNail(cc.p(lastPosX, this._posY));

            this._layer.addRole(lastNail);
            lastPosX += lastNail.getWidth();
        }
    },
});